package leetcode.code2200;

import java.util.ArrayList;
import java.util.List;

import leetcode.IDebug;

public class Solution implements IDebug {

	public List<Integer> findKDistantIndices(int[] nums, int key, int k) {
		int len = nums.length;
		int p = 0;
		int L = 0, R = -1;
		List<Integer> ans = new ArrayList<>();
		while (p <= len) {
			if (p == len || nums[p] == key) {
				int l = Math.max(0, p - k);
				int r = Math.min(len - 1, p + k);
				if (p != len && R == -1) {
					L = Math.max(0, p - k);
					R = Math.min(len - 1, p + k);
				} else if (p != len && merge(L, R, l, r)) {
					R = r;
				} else {
					for (int i = L; i <= R; i++) {
						ans.add(i);
					}
					L = l;
					R = r;
				}
			}
			p++;
		}
		return ans;
	}

	private boolean merge(int L, int R, int l, int r) {
		int left = Math.max(L, l);
		int right = Math.min(R, r);
		if (left <= right) {
			return true;
		}
		return false;
	}

	@Override
	public void debug4() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug3() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug2() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug1() {
		// TODO Auto-generated method stub

	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub

	}

}
